﻿using System.Drawing;
using System.Windows.Forms;
using System;

namespace DotNet.Common
{
    public class LogTextBox : TextBox
    {
        public LogTextBox()
        {
            base.Multiline = true;
            base.ReadOnly = true;
            base.BackColor = SystemColors.Window;
            base.ScrollBars = ScrollBars.Both;

            LoadMenu();
        }

        public void Add(string logInfo)
        {
            this.Text += (logInfo + "\r\n");
            this.SelectionStart = this.TextLength;
            this.ScrollToCaret();
        }

        public new bool WordWrap
        {
            get
            {
                return base.WordWrap;
            }
            set
            {
                base.WordWrap = value;
                _menuWordWrap.Checked = value;
            }
        }

        #region 菜单项加载
        private System.Windows.Forms.ContextMenuStrip _logTextBoxMenuStrip;
        private System.Windows.Forms.ToolStripMenuItem _menuTextClear;
        private System.Windows.Forms.ToolStripMenuItem _menuSelectAll;
        private System.Windows.Forms.ToolStripMenuItem _menuCopy;
        private System.Windows.Forms.ToolStripMenuItem _menuWordWrap;
        private ToolStripSeparator _menuseparator1;
        private ToolStripSeparator _menuseparator2;
        private void LoadMenu()
        {
            this._logTextBoxMenuStrip = new System.Windows.Forms.ContextMenuStrip();
            this._menuTextClear = new System.Windows.Forms.ToolStripMenuItem();
            this._menuWordWrap = new System.Windows.Forms.ToolStripMenuItem();
            this._menuSelectAll = new System.Windows.Forms.ToolStripMenuItem();
            this._menuCopy = new System.Windows.Forms.ToolStripMenuItem();
            this._menuseparator1 = new ToolStripSeparator();
            this._menuseparator2 = new ToolStripSeparator();

            this._logTextBoxMenuStrip.SuspendLayout();

            // 
            // LogTextBoxMenuStrip
            // 
            this._logTextBoxMenuStrip.Items.AddRange(new System.Windows.Forms.ToolStripItem[] {
            this._menuTextClear,
            this._menuseparator1,
            this._menuSelectAll,
            this._menuCopy,
            this._menuseparator2,
            this._menuWordWrap});
            this._logTextBoxMenuStrip.Name = "LogTextBoxMenuStrip";
            this._logTextBoxMenuStrip.Size = new System.Drawing.Size(153, 70);
            this._logTextBoxMenuStrip.Opened += new EventHandler(_logTextBoxMenuStrip_Opened);
            // 
            // menuTextClear
            // 
            this._menuTextClear.Name = "menuTextClear";
            this._menuTextClear.Size = new System.Drawing.Size(152, 22);
            this._menuTextClear.Text = "全部清除(&R)";
            this._menuTextClear.Click += new System.EventHandler(this.menuTextClear_Click);
            // 
            // _menuSelectAll
            // 
            this._menuSelectAll.Name = "menuSelectAll";
            this._menuSelectAll.Size = new System.Drawing.Size(152, 22);
            this._menuSelectAll.Text = "选择全部(&A)";
            this._menuSelectAll.Click += new System.EventHandler(this.menuSelectAll_Click);
            // 
            // _menuCopy
            // 
            this._menuCopy.Name = "menuCopy";
            this._menuCopy.Size = new System.Drawing.Size(152, 22);
            this._menuCopy.Text = "复制(&C)";
            this._menuCopy.Click += new System.EventHandler(this.menuCopy_Click);
            // 
            // menuWordWrap
            // 
            this._menuWordWrap.Checked = true;
            this._menuWordWrap.CheckOnClick = true;
            this._menuWordWrap.CheckState = System.Windows.Forms.CheckState.Checked;
            this._menuWordWrap.Name = "menuWordWrap";
            this._menuWordWrap.Size = new System.Drawing.Size(152, 22);
            this._menuWordWrap.Text = "自动换行(&L)";
            this._menuWordWrap.Click += new System.EventHandler(this.menuWordWrap_Click);
            // 
            // separator1
            // 
            _menuseparator1.Name = "separator1";
            _menuseparator1.Size = new System.Drawing.Size(109, 6);
            // 
            // separator2
            // 
            _menuseparator2.Name = "separator2";
            _menuseparator2.Size = new System.Drawing.Size(109, 6);

            this._logTextBoxMenuStrip.ResumeLayout(false);

            this.ContextMenuStrip = _logTextBoxMenuStrip;
        }

        void _logTextBoxMenuStrip_Opened(object sender, EventArgs e)
        {
            _menuSelectAll.Enabled = _menuTextClear.Enabled = (this.TextLength != 0);
            _menuCopy.Enabled = (this.SelectionLength != 0);
        }

        private void menuTextClear_Click(object sender, EventArgs e)
        {
            this.Clear();
        }

        private void menuSelectAll_Click(object sender, EventArgs e)
        {
            this.Focus();
            this.SelectAll();
        }

        private void menuCopy_Click(object sender, EventArgs e)
        {
            this.Copy();
        }

        private void menuWordWrap_Click(object sender, EventArgs e)
        {
            this.WordWrap = _menuWordWrap.Checked;
        }
        #endregion
    }
}
